...
)Description: The spread operator allows you to expand an iterable (like an array or string) into individual elements.
Usage: Commonly used for merging arrays or spreading elements in function arguments.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const merged = [...arr1, ...arr2]; // Merges two arrays
console.log(merged); // Output: [1, 2, 3, 4, 5, 6]
...
)Description: The rest operator collects multiple elements into a single array parameter. It is used in function parameter lists.
Usage: Useful for functions that can take an indefinite number of arguments.
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10
??
)Description: Returns the right-hand operand when the left-hand operand is
null
or undefined
. Otherwise, it returns the left-hand operand.
Usage: Provides a way to set default values without overwriting falsy values like
0
or ""
.
let foo;
let bar = foo ?? 'default';
console.log(bar); // Output: "default"
?.
)Description: Allows you to safely access deeply nested properties without checking if each reference is valid.
Usage: Prevents runtime errors when trying to access properties on null
or
undefined
.
const user = { name: "Alice", address: { city: "Wonderland" } };
const city = user.address?.city; // "Wonderland"
const zip = user.address?.zip; // undefined
console.log(city);
console.log(zip);
&&=
, ||=
, ??=
)Description: These combine logical operations with assignment.
Usage: Useful for conditionally assigning values based on existing truthiness.
let x = 1;
x ||= 2; // x remains 1
let y = 0;
y ||= 2; // y becomes 2
let z = null;
z ??= "default"; // z becomes "default"
console.log(x);
console.log(y);
console.log(z);
// Output: 1, 2, "default"
n
)Description: The BigInt type allows representation of integers larger than the maximum safe integer limit in JavaScript.
Usage: Useful for high-precision arithmetic.
const big = 123456789012345678901234567890n; // BigInt
console.log(big); // Output: 123456789012345678901234567890n